package code.structure.module.scene.view.iso.product
{
	import code.structure.module.configs.data.ItemConfigData;
	import code.structure.module.scene.model.data.SceneItemData;
	import code.structure.module.scene.view.iso.product.base.ProductedCIsoObject;
	import code.untils.GameConst;
	import code.untils.ProductStatusConst;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 *耕地 上面可以种植植物 
	 * @author Yuri
	 * 
	 */
	public class ProductPloughIsoObject extends ProductedCIsoObject
	{
		/**
		 *耕地 
		 */
		private  var _ploughBitMap:Bitmap;
		
		private var _seedOffestP:Point = new Point();
		
		/**
		 *土地素材 
		 */
		private var _ploughBitmapList:Array = [];
		/**
		 *土地素材是否加载完成 
		 */
		private var _isPloughMateralLoadComplete:Boolean = false;
		
		private var _prepareSeedData:ItemConfigData;
		public function ProductPloughIsoObject()
		{
			super();
			isHaveWithered = true;
			_ploughBitMap = new Bitmap();
			_mainBitMapContainer.addChildAt(_ploughBitMap,0);
			selfTipEnable = false;
			WITHERED_TIME = GameConst.WITHERED_TIME_PLANT;
		}
		
		override protected function refresh():void{
   			id = data.id;
			//这里只做刷新素材
			floorXCount = data.data.sizeX;
			floorZCount = data.data.sizeZ;
			direction = data.direction;
			
			refreshMaterial(data.data.sPath,"SCENE");
			if(id) refreshProductData();
		} 
		
		/**
		 *造好回调 
		 * 
		 */
		override public function initBuildCallFun():void{
			data.status = 4;
		}
		
		/**
		 *播种返回 
		 * @return 
		 * 
		 */
		public function plantSeedCallFun(pData:ItemConfigData=null):void{
			if(!pData) pData = prepareSeedData;
			productShopItemData = pData;
			data.bindingData = pData;
			data.bind_id = pData.id;
			data.endTime = productShopItemData.productAttachData.growTime;
			data.growTimes = productShopItemData.productAttachData.growCount;
			data.status = 4;
			data.outputData = pData.productAttachData.outputData;
			data.output_num = pData.productAttachData.outputNum;
			data.output_id = pData.productAttachData.outputId;
			refreshProductData();
			refreshSeedMaterial();
			
			prepareSeedData = null;
		}
		
		/**
		 *收获回调 
		 * 
		 */
		override public function harvestCallFun():void{
			clearPlantInfo();
			cancelWitheredProgress();
		}
		
		override public function witheredCallFun():void{
			clearPlantInfo();
		}
		
		/**
		 *清除种子的任何信息 
		 * 
		 */
		private function clearPlantInfo():void{
			/*if(_bitmap && _mainBitMapContainer.contains(_bitmap)){
				_mainBitMapContainer.removeChild(_bitmap);
				_bitmap.bitmapData = null;
			}*/
			clearAnimation();
			clearBody();
			productShopItemData = null;
			data.bindingData = null;
			data.bind_id = 0;
			data.output_id = 0;
			data.output_num = 0;
			data.outputData = null;
			_bitmapList = [];
			_ripeAnimationList = [];
			_witheredAnimationList = [];
			_productAnimationList = [];
			isMaterialLoadComplete = false;
			_frame = 0;
			if(_ploughBitmapList && _ploughBitmapList.length>0)
				verticalHeight = _ploughBitmapList[0][1];
			productStatus = ProductStatusConst.PRODUCT_INIT_STATUS;
			
			if(!selfTipEnable) return;
			createTip();
		}
		
		/**
		 *取消生产 
		 * 
		 */
		override public function cancelProduct():void{
			if(productStatus==ProductStatusConst.PRODUCT_PROGRESS_STATUS){
				clearPlantInfo();
				cancelProductProgress();
			}
		}
		
		/**
		 * 
		 * 刷新生产信息 进行后续操作
		 */
		override protected function refreshProductData():void
		{
			if(!productShopItemData) return;
			if(data.growTimes<=0){
				clearPlantInfo();
			}else{
				if(data.endTime<=0){
					checkIfWithered();
				}else{
					product();
				}
			}
		}
		
		/**
		 *刷新种子素材 
		 * 
		 */
		private function refreshSeedMaterial():void{
			if(productShopItemData && _isPloughMateralLoadComplete){
				refreshMaterial(productShopItemData.sPath,"SCENE");
			}else if(!productShopItemData){
				//无种子的情况下
				_isMaterialLoadComplete = true;
				productStatus = ProductStatusConst.PRODUCT_INIT_STATUS;
				mainBodyLoadCompleteNext();
			}
		}
		
		/**
		 *转换成位图回调 
		 * @param subKey
		 * @param bitmapArr
		 * 
		 */
		override protected function onCovertCompleteFun(bitmapArr:Array):void
		{
			if(!_isPloughMateralLoadComplete){
				//土壤素材加载完成 
				_isPloughMateralLoadComplete = true;
				_ploughBitmapList = bitmapArr;
				if(_ploughBitmapList.length>0)
					verticalHeight = _ploughBitmapList[0][1];
				refreshPloughMaterial(1);
				refreshSeedMaterial();//加载种子素材
			}else{
				super.onCovertCompleteFun(bitmapArr);
			}
		}
		
		/**
		 *耕地 跳帧
		 * @param frame
		 * 
		 */
		private function refreshPloughMaterial(frame:int):void{
			if(_ploughBitmapList && frame<=_ploughBitmapList.length){
				_ploughBitMap.x = offsetX + _ploughBitmapList[frame-1][0];
				_ploughBitMap.y = offsetY + _ploughBitmapList[frame-1][1];
				_ploughBitMap.bitmapData = _ploughBitmapList[frame-1][2];
			}
			
			_isChangeFacade = true;
		}
		
		override public function set data(value:SceneItemData):void{
			_data = value;
			if(value){
				id = value.id;
				productShopItemData = data.bindingData;
				refresh();
			}
		}

		/**
		 *准备要播种的对象 
		 */
		public function get prepareSeedData():ItemConfigData
		{
			return _prepareSeedData;
		}

		/**
		 * @private
		 */
		public function set prepareSeedData(value:ItemConfigData):void
		{
			_prepareSeedData = value;
		}
	}
}